//
//  StatusViewCell.swift
//  weibo
//
//  Created by yellow on 15/10/20.
//  Copyright © 2015年 yellow. All rights reserved.
//

import UIKit


//定义常量
let statusIconWidth = 35
/// 控件间的间距
let widgetMargin = 12


class StatusViewCell: UITableViewCell {
    
    //接收微博数据
    var viewModel:StatusDetailViewModel?{
        didSet{
            //将微博其他数据再分发给它的子视图
            topView.viewModel = viewModel
            //设置微博内容
            body.text = viewModel?.status.text
            
            //传递模型后，会执行sizeToFit(),计算出需要的高度
            statusPictureView.model = viewModel
            //配图视图有了大小后，需要使用自动布局更新下约束，宽高为自身的宽高
            
//            statusPictureView.snp_updateConstraints { (make) -> Void in
//                make.width.equalTo(statusPictureView.bounds.width)
//                make.height.equalTo(statusPictureView.bounds.height)
//                
//                //当没有配图是，就取消statusPictureView距离正文的间距
//                let top = statusPictureView.bounds.height>0 ? widgetMargin : 0
//                make.top.equalTo(body.snp_bottom).offset(top)
//            }
        }
    }


    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style:style, reuseIdentifier: reuseIdentifier)
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //视图属性
    
    //cell分隔视图
    lazy var seprateView:UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(white: 0.8, alpha: 0.8)
        return view
    }()
    

    lazy var topView:CellTopView = CellTopView()
    lazy var body:UILabel = UILabel(content: "", color: UIColor.darkGrayColor(), fontSize: 12, maxLayoutWidth: UIScreen.mainScreen().bounds.size.width - CGFloat(2*widgetMargin))
    
    //微博配图
    lazy var statusPictureView:StatusPictureView = StatusPictureView()
    lazy var bottom:CellBottomView = CellBottomView()
}

// 页面布局
extension StatusViewCell{
    
    func setUI(){
        contentView.addSubview(topView)
        contentView.addSubview(body)
        contentView.addSubview(statusPictureView)
        contentView.addSubview(bottom)
        
        
        //自动布局
        
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(statusIconWidth+2*widgetMargin)
        }
        
        body.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.topView.snp_bottom).offset(widgetMargin)
            make.left.equalTo(self.topView.snp_left).offset(widgetMargin)
        }
        
        //底部视图
        bottom.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusPictureView.snp_bottom).offset(widgetMargin)
            make.left.equalTo(self.contentView.snp_left)
            make.right.equalTo(self.contentView.snp_right)
            make.height.equalTo(34)
        }

    }
}


// MARK: - 对外接口
extension StatusViewCell{
    
    //根据底部视图的最大Y值来计算当前cell的高度
    func rowHeight(vm:StatusDetailViewModel) -> CGFloat{
        viewModel = vm
        
        //强制更新自动布局的约束
        contentView.layoutIfNeeded()
        
        return CGRectGetMaxY(bottom.frame)
    }
}
